
$(document).ready(function() {
	
	for(var y=2000; y>=1950; y--) {
		var selY = $("#sel-Y");
//		selY.append(new Option(y, y));
		var newOption = document.createElement("OPTION");
        newOption.text = y;
        newOption.value = y;
        selY[0].options.add(newOption);
	};
	for(var m=1; m<=12; m++) {
		var selM = $("#sel-M");
//		selM.append(new Option(m, m));
		var newOption = document.createElement("OPTION");
        newOption.text = m;
        newOption.value = m;
        selM[0].options.add(newOption);
	};
	for(var d=1; d<=31; d++) {
		var selD = $("#sel-D");
//		selD.append(new Option(d, d));
		var newOption = document.createElement("OPTION");
        newOption.text = d;
        newOption.value = d;
        selD[0].options.add(newOption);
	};
	
	//初始化性别
	$("input[name='sex']").each(function(index, element) {
        var sex = $.parseJSON($.cookie("User")).sex;
		if($(element).val() == sex) {
			$(element).prop("checked", true);
		}
    });
	
	//初始化生日
	var birthArr = $.parseJSON($.cookie("User")).birthday.split(",");	
	$("#sel-Y option").each(function(index, element) {
        if($(element).val() == birthArr[0]) {
			$(element).prop("selected", true);
		}
    });
	$("#sel-M option").each(function(index, element) {
        if($(element).val() == birthArr[1]) {
			$(element).prop("selected", true);
		}
    });
	$("#sel-D option").each(function(index, element) {
        if($(element).val() == birthArr[2]) {
			$(element).prop("selected", true);
		}
    });
	
	/* 修改用户图像 */
	$("#change-user-img").click(function(e) {
        Utils.checkDialog("<span style='display:block; margin-left:80px;'>选择图片：推荐尺寸125x125像素</span>"+
			"<div id='sel-img-div' style='display:inline-block; margin-left:110px; margin-top:10px;'>"+
    		"<script>Utils.upLoadImg('sel-img-div', 'up-img-sq.html', 1);</script></div>", 
			"修改", "取消", 
			function() {
				var userImg = $("#imgUrl_sel-img-div").val();
				if($.trim(userImg) == "") {
					Utils.msgDialog("请先选择图片并上传，再进行修改！");
					return;
				}
				
				var param = {
					userImg: userImg,
				};
				AQ.callAQ("changeUserImg.do", param, function(result) {
					$("#user-img-pic").prop("src", userImg);
				}, function(err, code){
					Utils.msgDialog(err + "["+ code +"]");
				});
			}, function() {}
		);
    });
	
	$("#submit-baseinfo").click(function(e) {
		var sex = $("input[name='sex']:checked").val();
		var birthday = $("#sel-Y").val() +","+ $("#sel-M").val() +","+ $("#sel-D").val();
        var qq = $.trim($("#qq-bi").val());
		var qqReg = /^[0-9]{5,15}$/;
		if(!qqReg.test(qq) || qq=="") {
			Utils.msgDialog("×  QQ号格式不正确！");
			return false;
		}
		
        var param = {
			sex: sex,
			birthday: birthday,
			qq: qq,
		};
		AQ.callAQ("updateBaseInfo.do", param, function(result) {
			Utils.msgDialog("操作成功！");
			location.replace("user-center.html");
		}, function(err, code){
			Utils.msgDialog(err + "["+ code +"]");
		});
    });
	
});